﻿var activeSecondNav;
var currentSecondNav;
var navTimeout;
var firstNavLeft;
var ItemSelected;

function constructDynamicMenu() {
    //alert("siteBase = " + siteBase);
    //alert("themeName = " + themeName);

    var i, j;

    var nav = document.getElementById("first-nav-holder");

    var firstUL = nav.getElementsByTagName("ul")[0];

    //Create the menu items dynamically here...
    var MenuItemsInputStr = nav.getElementsByTagName("input")[0];
    //alert(MenuItemsInputStr.value);

    var MenuItemsArray = MenuItemsInputStr.value.split(";");
    ItemSelected = false;

    //check if there is any previously selected item.
    var SelectedMenuItem = nav.getElementsByTagName("input")[1];
    if (SelectedMenuItem.value == "") {
        //SelectedMenuItem.value = PortalClientService.GetSelectedMenuItemFromCache() //Ajax call
        var resultText = GetSynchronousJSONResponse(
                        siteBase + 'PortalClientService.asmx/GetSelectedMenuItemFromCache', null); //Jason call...

        //alert(resultText);
        SelectedMenuItem.value = resultText.split("\"")[3];
    }

    for (i = 0; i < MenuItemsArray.length - 1; i++) {
        //alert(MenuItemsArray[i]);
        var MenuItems = MenuItemsArray[i].split("&");
        var CurrentUL = firstUL;
        var ParentLI;

        for (j = 0; j < MenuItems.length; j++) {
            //alert(MenuItems[j]);
            var MenuItemAttribs = MenuItems[j].split(",");
            //alert(MenuItemAttribs[0]);
            //alert(MenuItemAttribs[1]);
            //alert(MenuItemAttribs[2]);

            if (j == 1) { //second level menu items begin.
                var secondUL = document.createElement('ul');
                ParentLI.appendChild(secondUL);

                CurrentUL = secondUL;
                MenuItemAttribs[0] = "  " + MenuItemAttribs[0]; //Add an extra space for first item...
            }
            else if (j == 0) { //Add first nav seperator for first level menu items only
                var listItem = document.createElement('li');
                listItem.setAttribute('class', 'AspNet-Menu-Separator');

                var imgItem = document.createElement('img');
                imgItem.setAttribute('alt', 'Separator');
                imgItem.setAttribute('src', siteBase + 'app_themes/' + themeName + '/images/Navigation/mainNavDivider.gif');
                listItem.appendChild(imgItem);
                //alert('image path = ' + siteBase + 'app_themes/' + themeName + '/images/Navigation/secondNavDivider.gif');

                if (i >= 1) { CurrentUL.appendChild(listItem) };
            }
            else { //Add SecondNavDivider for second level menu items
                var secondnav_divider_listItem = document.createElement('li');
                secondnav_divider_listItem.setAttribute('class', 'AspNet-Menu-Separator');

                var secondnav_divider_imgItem = document.createElement('img');
                secondnav_divider_imgItem.setAttribute('alt', 'Separator');
                secondnav_divider_imgItem.setAttribute('src', siteBase + 'app_themes/' + themeName + '/images/Navigation/secondNavDivider.gif');
                secondnav_divider_listItem.appendChild(secondnav_divider_imgItem);

                CurrentUL.appendChild(secondnav_divider_listItem);
            }

            var listItem1 = document.createElement('li');

            var anchor = document.createElement('a');
            anchor.setAttribute('title', MenuItemAttribs[1]);
            anchor.href = MenuItemAttribs[2];
            //addint menu text

            //anchor.innerText = MenuItemAttribs[0];
            //Nawal
            anchor.innerHTML = MenuItemAttribs[0];


            //Set Initial styles.
            if (j == 0) {
                ParentLI = listItem1;
                if (SelectedMenuItem.value == MenuItemAttribs[1]) {
                    //alert("Inside selected menu item condition");
                    listItem1.setAttribute('class', 'AspNet-Menu-WithChildren AspNet-Menu-ChildSelected');
                    listItem1.className = 'AspNet-Menu-WithChildren AspNet-Menu-ChildSelected';
                    anchor.setAttribute('class', 'AspNet-Menu-ChildSelected');
                    anchor.className = 'AspNet-Menu-ChildSelected';
                    ItemSelected = true;
                }
                else {
                    listItem1.setAttribute('class', 'AspNet-Menu-WithChildren');
                    listItem1.className = 'AspNet-Menu-WithChildren';
                }
            }
            else {
                listItem1.setAttribute('class', 'AspNet-Menu-Leaf');
                listItem1.className = 'AspNet-Menu-Leaf';
            }

            listItem1.appendChild(anchor);
            CurrentUL.appendChild(listItem1);
        }
    }

    if (ItemSelected == false) // if no item is selected
    {
        //select the first second level menu item.
        for (i = 0; i < firstUL.childNodes.length; i++) {
            if (firstUL.childNodes[i].nodeName == "#text") {
                continue;
            }

            firstLi = firstUL.childNodes[i];
            secondUL = firstLi.getElementsByTagName("ul")[0];

            if (secondUL != null) {
                firstLi.setAttribute('class', 'AspNet-Menu-WithChildren AspNet-Menu-ChildSelected');
                firstLi.className = 'AspNet-Menu-WithChildren AspNet-Menu-ChildSelected';
                break;
            }
        }
    }

    //can fix the strange dispay problem in IE
    nav.style.display = "block";
    sleep(150); //Give time to render the UI elements created!    
}

initNav = function() {
    var i, j;
    var a;

    var nav = document.getElementById("first-nav-holder");

    var firstUL = nav.getElementsByTagName("ul")[0];

    constructDynamicMenu();

    //Adjust the styles.
    for (i = 0; i < firstUL.childNodes.length; i++) {
        if (firstUL.childNodes[i].nodeName == "#text") {
            continue;
        }

        var firstLi = firstUL.childNodes[i];
        firstNavLeft = -firstLi.offsetLeft + "px";
        var secondUL = firstLi.getElementsByTagName("ul")[0];

        if (secondUL != null) {

            //alert(secondUL);
            secondUL.style.left = -firstLi.offsetLeft + "px";
            //alert(secondUL.style.left);
            if (firstLi.className.match(/AspNet-Menu-Selected|AspNet-Menu-ChildSelected/)) {
                activeSecondNav = secondUL;

                //setVisibility(secondUL,"visible");
                secondUL.style.visibility = "visible";
            }
            var secondNavLinks = secondUL.getElementsByTagName("a");
            for (j = 0; j < secondNavLinks.length; j++) {
                a = secondNavLinks[j];
                a.onmouseover = function() { onSecondNavHover() };
                a.onmouseout = onNavHout;
                a.onclick = function() { onSecondNavClick(this) };
            }
        }

        a = firstLi.getElementsByTagName("a")[0];
        if (a != null) {
            a.onmouseover = function() { onFirstNavHover(this) };
            a.onmouseout = onNavHout;
            if (a.href.indexOf('#nolink') > 0) {
                a.onclick = "javascript:";
                a.href = "#";
            }
            else {
                a.onclick = function() { onFirstNavClick(this) };
            }
        }
    }

    //store the current rollover second nav
    if (activeSecondNav != null) currentSecondNav = activeSecondNav;


    //can fix the strange dispay problem in IE
    nav.style.display = "block";
}

onFirstNavHover = function(a) {
    clearTimeout(navTimeout);

    var ul = a.parentNode.getElementsByTagName("ul")[0];

    //has second nav
    if (ul != undefined) {
        //rollover the current active node
        if (ul == activeSecondNav) {
            //setVisibility(currentSecondNav,"hidden");
            if (currentSecondNav != null) currentSecondNav.style.visibility = "hidden";
            
            //setVisibility(activeSecondNav,"visible");
            activeSecondNav.style.visibility = "visible";
        }
        //rollover other node
        else {
            //the active node has second nav
            if (activeSecondNav != null) {
                //setVisibility(activeSecondNav,"hidden");
                activeSecondNav.style.visibility = "hidden";

                //setVisibility(ul,"visible");
                ul.style.visibility = "visible";
            }
            //the active node has no second nav
            else {
                //setVisibility(currentSecondNav,"hidden");
                if (currentSecondNav != null) currentSecondNav.style.visibility = "hidden";


                //setVisibility(ul,"visible");
                ul.style.visibility = "visible";
            }
        }
    }
    //no second nav
    else {
        //setVisibility(activeSecondNav,"hidden");
        if (activeSecondNav != null) activeSecondNav.style.visibility = "hidden";

        //setVisibility(currentSecondNav,"hidden");
        if (currentSecondNav != null) currentSecondNav.style.visibility = "hidden";
    }

    //store the current rollover second nav
    currentSecondNav = ul;
}

onSecondNavHover = function() {
    clearInterval(navTimeout);
}

onNavHout = function() {
    if (activeSecondNav != null) {
        navTimeout = setTimeout(function() {
            //setVisibility(activeSecondNav,"visible");
            activeSecondNav.style.visibility = "visible";

            if (activeSecondNav != currentSecondNav) {
                //setVisibility(currentSecondNav,"hidden");
                if (currentSecondNav != null) currentSecondNav.style.visibility = "hidden";
            }
        }, 1000);
    }
    else {
        if (currentSecondNav != null) {
            navTimeout = setTimeout(function() {
                /*setVisibility(currentSecondNav,"hidden");*/currentSecondNav.style.visibility = "hidden";
            }, 1000);
        }
    }
}

hideThirdNavItems = function() {

    var list = $$(".ThirdNav .AspNet-Menu-NonLink");

    for (var i = 0; i < list.length; i++) {
        list[i].parentNode.style.display = "none";
    }
}

onFirstNavClick = function(a) {
    var li = a.parentNode;

    var nav = document.getElementById("first-nav-holder");
    var firstUL = nav.getElementsByTagName("ul")[0];

    for (i = 0; i < firstUL.childNodes.length; i++) {
        if (firstUL.childNodes[i].nodeName == "#text") {
            continue;
        }

        var firstLi = firstUL.childNodes[i];
        var secondUL = firstLi.getElementsByTagName("ul")[0];

        if (secondUL != null) {
            if (firstLi.className.match(/AspNet-Menu-Selected|AspNet-Menu-ChildSelected/)) {
                activeSecondNav = secondUL;
                firstLi.className = 'AspNet-Menu-Leaf';
                firstLi.setAttribute('class', 'AspNet-Menu-Leaf');
                break;
            }
        }
    }
    li.setAttribute('class', 'AspNet-Menu-WithChildren AspNet-Menu-ChildSelected');
    li.className = 'AspNet-Menu-WithChildren AspNet-Menu-ChildSelected';

    var SelectedMenuItem = nav.getElementsByTagName("input")[1];
    if (SelectedMenuItem.id == "ctl00_Menu1_SelectedMenuItem") {
        nav.getElementsByTagName("input")[1].value = li.getElementsByTagName("a")[0].title;
        var result = GetSynchronousJSONResponse(siteBase + 'PortalClientService.asmx/PutSelectedMenuItemInCache',
                          '{"val":"' + li.getElementsByTagName("a")[0].title + '"}');
    }
    //alert(SelectedMenuItem.value);
}
onSecondNavClick = function(a) {

    var li = a.parentNode.parentNode.parentNode; //trace back to first level nav.

    var nav = document.getElementById("first-nav-holder");
    var SelectedMenuItem = nav.getElementsByTagName("input")[1];
    if (SelectedMenuItem.id == "ctl00_Menu1_SelectedMenuItem") {
        nav.getElementsByTagName("input")[1].value = li.getElementsByTagName("a")[0].title;
        var result = GetSynchronousJSONResponse(siteBase + 'PortalClientService.asmx/PutSelectedMenuItemInCache',
                          '{"val":"' + li.getElementsByTagName("a")[0].title + '"}');
    }
    //alert(SelectedMenuItem.value);    
}

function GetSynchronousJSONResponse(url, postData) {
    var xmlhttp = null;
    if (window.XMLHttpRequest)
        xmlhttp = new XMLHttpRequest();
    else if (window.ActiveXObject) {
        if (new ActiveXObject("Microsoft.XMLHTTP"))
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        else
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }

    //Nawal
    try
    {
     xmlhttp.open("POST", url, false); //false means synchronous
        xmlhttp.setRequestHeader("Content-Type", "application/json; charset=utf-8");
     xmlhttp.send(postData);
    }
    catch (err)
    {
        var responseText = "";
        return responseText;
    }
    var responseText = xmlhttp.responseText;
    //alert(responseText);
    
    return responseText;
}

function sleep(milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds) {
            break;
        }
    }
}